package com.company;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.jna.StringArray;
import com.sun.org.apache.xerces.internal.xs.StringList;
import com.utils.excel.ExcelHelper;
import com.utils.sqlite.SqliteHelper;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.sql.SQLException;

public class Main {

    public static void main_sqlite(String productType, String folder){
        SqliteHelper sqliteHelper;
        sqliteHelper = new SqliteHelper();
        sqliteHelper.connectTo("d:\\淘宝数据-"+ productType +".db");
        String createTableString = "CREATE TABLE IF NOT EXISTS phone ("
                + "ID    INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "TITLE TEXT(1024), "
                + "SHOP  TEXT(256),"
                + "BUY   TEXT"
                + ");";
        sqliteHelper.executeUpdate(createTableString);
        sqliteHelper.destroyed();

        TaobaoCrawler taobaoCrawler = new TaobaoCrawler(productType);
        int itemNumber = taobaoCrawler.getProductNumber();
        int collectedNumber = 0;
        if (itemNumber >= 100){
            itemNumber = 100;
        }
        while(collectedNumber < itemNumber){
            JSONArray jsonArray = taobaoCrawler.getItems(collectedNumber);
            for( Object object : jsonArray ){
                JSONObject jsonObject1 = (JSONObject)object;
                sqliteHelper.connectTo(folder+"淘宝数据-"+ productType +".db");
                String insertString = "INSERT INTO phone (TITLE, SHOP, BUY) VALUES("
                        + "'"+ jsonObject1.getString("title") + "'"  + ", "
                        + "'"+ jsonObject1.getString("nick") +"'" + ", "
                        + "'"+ jsonObject1.getString("view_sales") +"'"
                        + ");";
                sqliteHelper.executeUpdate(insertString);
                sqliteHelper.destroyed();

                collectedNumber ++;
            }
        }
    }

    public static void main_excel(String productType, String folder){
        ExcelHelper excelHelper = new ExcelHelper(folder+"淘宝数据-" + productType + ".xlsx", productType, 0);
        excelHelper.insert(0, new String[]{"标题", "价格", "销量"});

        TaobaoCrawler taobaoCrawler = new TaobaoCrawler(productType);
        int itemNumber = taobaoCrawler.getProductNumber();
        int collectedNumber = 0;
        if (itemNumber >= 100){
            itemNumber = 100;
        }
        while(collectedNumber < itemNumber){
            JSONArray jsonArray = taobaoCrawler.getItems(collectedNumber);
            for( Object object : jsonArray ){
                JSONObject jsonObject1 = (JSONObject)object;

                String[] items = new String[]{
                        jsonObject1.getString("title"),
                        jsonObject1.getString("nick"),
                        jsonObject1.getString("view_sales"),
                };
                excelHelper.insert(collectedNumber+1, items);

                collectedNumber ++;
            }
            excelHelper.save();
        }
        excelHelper.close();
    }

    public static void main(String[] args) {
        //main_sqlite("手机", "d:\\");
        main_excel("手机", "d:\\");
    }
}
